/**
 * 回到顶部
 * */
import { onBeforeUnmount, onMounted, ref } from 'vue'

export const useToTop = <T extends HTMLElement>(dom: T | null) => {
  const scrollTop = ref(0)
  const toTop = () => {
    if (!dom) return
    scrollTop.value = dom.scrollTop
  }

  onMounted(() => {
    dom?.addEventListener('scroll', toTop)
  })
  onBeforeUnmount(() => {
    dom?.removeEventListener('scroll', toTop)
  })
  return {
    scrollTop,
    toTop
  }
}
